/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.client;

import com.tunnel.client.config.Config;
import com.tunnel.client.tcp.TcpDataReceiverListener;
import com.tunnel.client.tcp.TcpDataSenderManager;
import com.tunnel.client.tunnel.TunnelClient;
import com.tunnel.client.udp.UdpDataSenderManager;
import com.tunnel.common.util.CollectionUtil;
import com.tunnel.common.util.LogUtil;
import com.tunnel.common.util.StringUtil;

public class CRun {
	public static void main(String[] args) {
		String configFileUrl = null;
		if(args != null && args.length > 0){
			configFileUrl = args[0];
		}
		if(StringUtil.isEmpty(configFileUrl)){
			System.out.println("请指定tunnel-client配置文件");
			System.exit(0);
		}else{
			Config.init(configFileUrl);
		}
		
		//启动tcp监听
		new TcpDataReceiverListener().start();
		//启动tcp消息队列
		TcpDataSenderManager.startQueue();
		
		//启动udp发送客户端
		if(CollectionUtil.isNotEmpty(Config.UDP_LIST)){
			//配置了udp监听端口，就启动udp服务
			try {
				//启动udp消息发送队列
				UdpDataSenderManager.startQueue();
			} catch (Exception e) {
				LogUtil.e(e);
				System.exit(0);
			}
		}
		
		//启动tunnel客户端
		new TunnelClient().start();

		System.out.println("tunel-client v2.0");
	}
}
